//
//  BIZConsultorCellNew.m
//  biz
//
//  Created by zhangfuwei on 2017/4/7.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZConsultorCellNew.h"
#import "BIZConsultorListDataModel.h"
#import "BIZStarView.h"

//  视图间距
static CGFloat kMargin = 5;
static CGFloat kViewMargin = 15;
static CGFloat kEdgeMargin = 20;

@implementation BIZConsultorCellNew {
    UIImageView *_headImageView;
    UILabel *_nameLabel; // 姓名
    UILabel *_companyLabel;//  公司
    UILabel *_positionLabel; // 职位
    UILabel *_totalGradeLabel;// 评分
    UIImageView *_yearImgV; // 年限图标
    UILabel *_yearsLable; // 年限
    UILabel *_tradeLable; // 擅长业务
    UIImageView *_gradeImageView; // 星星视图
    BIZStarView      *_gradeImageVs;
    UIImageView *_arrowImgView; // 右侧箭头
    UIView      *_underSplitLine; // 底部分割线
    
    
    NSInteger   _imageNum; // 个数
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _imageNum = 1;
        [self loadCustomizedSubviews];
    }
    return self;
}

- (void)loadCustomizedSubviews {
    _headImageView = [[UIImageView alloc] init];
    _headImageView.layer.cornerRadius = 5;
    _headImageView.clipsToBounds = YES;
    
    
    _nameLabel = [[UILabel alloc] init];
    _positionLabel = [[UILabel alloc] init];
    _companyLabel = [[UILabel alloc] init];
    _totalGradeLabel = [[UILabel alloc] init];
    _yearsLable = [[UILabel alloc] init];
    _tradeLable = [[UILabel alloc] init];

    _positionLabel.textAlignment = NSTextAlignmentLeft;
    _yearsLable.textAlignment = NSTextAlignmentLeft;
    _tradeLable.numberOfLines = 0;
    
    
    _nameLabel.font = SYSTEMFONT(14);
    _positionLabel.font = SYSTEMFONT(13);
    _companyLabel.font = SYSTEMFONT(13);
    _totalGradeLabel.font = SYSTEMFONT(13);
    _yearsLable.font = SYSTEMFONT(12);
    _tradeLable.font = SYSTEMFONT(13);

    
    _nameLabel.textColor = HEXCOLOR(0x4c5c6c);
    _positionLabel.textColor = HEXCOLOR(0x010101);
    _companyLabel.textColor = HEXCOLOR(0x666666);
    _yearsLable.textColor = COLOR_NAVBAR_BLUE;
    _tradeLable.textColor = HEXCOLOR(0x2079ef);
    
    _yearImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yearWatch"]];
//    _gradeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"图层40"]];
    _gradeImageVs = [[BIZStarView alloc] initWithImageStr:@"Starcopia" imageRect:CGRectMake(0, 0, 15, 15) totalNum:_imageNum];
    _arrowImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"goto_arrow"]];
    _underSplitLine = [[UIView alloc] init];
    _underSplitLine.backgroundColor = HEXCOLOR(0xf9f9f9);
    
    [self.contentView addSubview:_headImageView];
    [self.contentView addSubview:_nameLabel];
    [self.contentView addSubview:_positionLabel];
    [self.contentView addSubview:_companyLabel];
    [self.contentView addSubview:_totalGradeLabel];
    [self.contentView addSubview:_yearsLable];
    [self.contentView addSubview:_tradeLable];
    
    [self.contentView addSubview:_gradeImageVs];
    
    [self.contentView addSubview:_yearImgV];
    [self.contentView addSubview:_gradeImageView];
    [self.contentView addSubview:_arrowImgView];
    [self.contentView addSubview:_underSplitLine];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _headImageView.frame = CGRectMake(kEdgeMargin, kEdgeMargin, 50, 50);
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headImageView.mas_right).offset(kMargin);
        make.top.equalTo(_headImageView);
        make.height.equalTo(@(20));
    }];
    
    [_positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(_nameLabel);
        make.centerY.equalTo(_nameLabel);
        make.left.equalTo(_nameLabel.mas_right).offset(kMargin);
        make.height.equalTo(@13);
    }];
    
    [_yearImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_positionLabel.mas_right).offset(kMargin);
//        make.bottom.equalTo(_nameLabel);
        make.centerY.equalTo(_nameLabel);

        make.height.with.equalTo(@13);
    }];
    // 年限
    [_yearsLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_yearImgV.mas_right).offset(kMargin);
//        make.bottom.equalTo(_nameLabel);
        make.centerY.equalTo(_nameLabel);
        make.height.equalTo(@13);
    }];
    
//    CGFloat gradeImageW = 20*_imageNum;
    CGFloat gradeImageW = 20*5;
    [_gradeImageVs mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_nameLabel.mas_bottom).offset(kMargin);
        make.height.equalTo(@20);
        make.width.equalTo(@(gradeImageW));
    }];
    
    
    [_totalGradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_gradeImageVs.mas_right).offset(kViewMargin);
        make.top.equalTo(_nameLabel.mas_bottom).offset(kMargin);
        make.height.equalTo(@20);
    }];
    
    [_companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_gradeImageVs.mas_bottom);
    }];
    
    [_tradeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_companyLabel.mas_bottom).offset(kMargin);
        make.right.equalTo(self.contentView).offset(-1*kViewMargin);
    }];
    [_underSplitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.equalTo(@10);
        make.top.equalTo(_tradeLable.mas_bottom).offset(1*kViewMargin);
    }];
    
    [_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-1*kViewMargin);
        make.height.equalTo(@20);
        make.width.equalTo(@10);
        make.top.equalTo(self.contentView).offset(35);
    }];
}


-(void)setDataModel:(BIZConsultorListDataModel *)dataModel {
    _dataModel = dataModel;
    
    [SDWebImageTool imageView:_headImageView imageName:dataModel.headImg placeHolder:@"default-head" showProgress:NO];
    _nameLabel.text = dataModel.cname;
    _positionLabel.text = dataModel.position;
    _companyLabel.text = dataModel.fname;
    NSString *str = @"综合评分: ";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%.1f分",str,dataModel.score] attributes:@{NSForegroundColorAttributeName:HEXCOLOR(0xffa200)}];
    
    
    [attStr setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, str.length)];
    
    _totalGradeLabel.attributedText = attStr;
    _yearsLable.text = [NSString stringWithFormat:@"%@年",dataModel.years];
    _tradeLable.text = [dataModel.trade stringByReplacingOccurrencesOfString:@"," withString:@" "];
    
    _imageNum = dataModel.score;
    _gradeImageVs.imageNum = _imageNum;
    
    [self layoutIfNeeded];

}

@end
